import {
  reqAddSpu,
  reqDeleteSpu,
  reqGetSpuList,
  reqUpdateSpu,
  reqGetTradeMarkSelect,
  reqBaseSaleAttrList,
  reqSpuImageList,
  reqSpuInfo,
  respuSaleAttrList
} from '@/api/spu'

const getDefaultState = () => {
  return {
    spuList: [],
    tradeMarkSelect: [],
    saleAttrList: [],
    spuId: null,
    spuImageList: [],
    spuInfo: {},
    saleAttrList: [],
    spuName: ''
  }
}

const state = getDefaultState()

const mutations = {
  SETSPULIST: (state, data) => {
    state.spuList = data
  },
  SETTRADEMARKSELECT: (state, data) => {
    state.tradeMarkSelect = data
  },
  SETBASESALEATTRLIST: (state, data) => {
    state.saleAttrList = data
  },
  SETSPUID: (state, spuId) => {
    state.spuId = spuId
  },
  SETSPUIMAGELIST: (state, data) => {
    state.spuImageList = data
  },
  SETSPUINFO: (state, data) => {
    state.spuInfo = data
  },
  SETSPUSALEATTRLIST: (state, data) => {
    state.saleAttrList = data
  },
  SPUNAME: (state, data) => {
    state.spuName = data
  }
}
const actions = {
  async getSpuList({ commit }, { page, limit, category3Id }) {
    const result = await reqGetSpuList(page, limit, category3Id)
    if (result.code !== 200) return
    commit('SETSPULIST', result.data)
  },
  async AddSpu({ commit }, obj) {
    const result = await reqAddSpu(obj)
    if (result.code == 200) {
      return Promise.resolve()
    } else {
      return Promise.reject('添加spu失败!!')
    }
  },
  async updateSpu({ commit }, obj) {
    const result = await reqUpdateSpu(obj)
    if (result.code == 200) {
      return Promise.resolve()
    } else {
      return Promise.reject('修改spu失败!!')
    }
  },
  async reqDeleteSpu({ commit }, spuId) {
    const result = await reqDeleteSpu(spuId)
    if (result.code == 200) {
      return Promise.resolve()
    } else {
      return Promise.reject('删除spu失败!!')
    }
  },
  async getTradeMarkSelect({ commit }, category3Id) {
    const result = await reqGetTradeMarkSelect(category3Id)
    if (result.code !== 200) return
    commit('SETTRADEMARKSELECT', result.data)
  },
  async getBaseSaleAttrList({ commit }) {
    const result = await reqBaseSaleAttrList()
    if (result.code !== 200) return
    commit('SETBASESALEATTRLIST', result.data)
  },
  async getSpuImageList({ commit }, spuId) {
    const result = await reqSpuImageList(spuId)
    if (result.code !== 200) return
    commit('SETSPUIMAGELIST', result.data)
  },
  async getSpuInfo({ commit }, spuId) {
    const result = await reqSpuInfo(spuId)
    if (result.code !== 200) return
    commit('SETSPUINFO', result.data)
  },
  async getSaleAttrList({ commit }, spuId) {
    const result = await respuSaleAttrList(spuId)
    if (result.code !== 200) return
    commit('SETSPUSALEATTRLIST', result.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
